﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace InfoPick.Lib.Entity
{
    public class Server : IXml
    {
        public Guid Guid { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public int Port { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public void FromXml(string xml)
        {
            XElement ele = XElement.Parse(xml);
            Guid = Guid.Parse(ele.Attribute("Guid").Value);
            Name = ele.Attribute("Name").Value;
            Address = ele.Attribute("Address").Value;
            Port = int.Parse(ele.Attribute("Port").Value);
            UserName = ele.Attribute("UserName").Value;
            string psd = ele.Attribute("Password").Value;
            Password = Encoding.Default.GetString(Convert.FromBase64String(psd));
        }

        public string ToXml()
        {
            XElement ele = new XElement("Server");
            ele.Add(new XAttribute("Guid", Guid.ToString()));
            ele.Add(new XAttribute("Name", Name));
            ele.Add(new XAttribute("Address", Address));
            ele.Add(new XAttribute("Port", Port));
            ele.Add(new XAttribute("UserName", UserName));
            ele.Add(new XAttribute("Password", Convert.ToBase64String(Encoding.Default.GetBytes(Password))));
            return ele.ToString();
        }
    }
}
